package com.my.controller;


import com.my.pojo.Folder;
import com.my.service.IFolderService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

@WebServlet("/user/folderController")
public class FolderController extends HttpServlet {
    private String folderPath;
    private String folderName;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        resp.setContentType("text/html;charset=utf-8");
        folderName = req.getParameter("folderName");
        folderName = req.getParameter("folderPath");
        boolean result = false;
        ServletContext sc = this.getServletContext();
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        IFolderService folderService = wac.getBean("folderService", IFolderService.class);
        boolean existFolder = folderService.isExistFolder(folderName);
        if(!existFolder){
            folderService.createFolder(folderPath,folderName);
             Folder folder = Folder.builder()
                     .folderName(folderName)
                     .folderPath(folderPath)
                     .folderDate(LocalDateTime.now().toString())
                     .creator("sa")
                     .folderDelete(new Integer(0)).build();
             boolean insertResult = folderService.saveFolder(folder);
             if(insertResult){
                 result=true;
             }
        }
        if(result){
            System.out.println("文件夹信息保存成功");
        }else {
            System.out.println("文件夹信息保存失败");
        }
        resp.sendRedirect("success.jsp");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        this.doGet(req,resp);
    }
}
